import {
	ref
} from "vue"
import {
	OneKeyLogin
} from "../api/modules/system";
import useStore from '@/stores'
import config from "../api/config";
import { getOaidAndImei } from "@/utils/index.js"

// #ifdef APP-PLUS
const jv = uni.requireNativePlugin('JG-JVerification');
// #endif
// const wechatCallback = ref()
export function initOauth(type) {
	return new Promise((resolve, reject) => {
		jv.isInitSuccess(result => {
			let isSucc = result.enable;
			if (isSucc) {
				resolve()
			} else {
				jv.init({
					timeout: 7000,
					isProduction: false,
				}, (res) => {
					if (res.code === 8000) {
						resolve()
					} else {
						reject('一键登录初始化失败')
					}
				})
			}
		})
	})
}

// #ifdef APP-PLUS
// 	校验网络环境
export function checkVerifyEnable() {
	return new Promise((resolve, reject) => {
		jv.checkVerifyEnable(result => {
			const isSupport = result.enable;
			resolve(isSupport)
		});
	})
}
// #endif

// #ifdef APP-PLUS
// 预取号
export function preLogin() {
	// if (callback) {
	// 	wechatCallback.value = callback
	// }
	const {
		globalStore
	} = useStore()
	return new Promise((resolve, reject) => {
		jv.preLogin(7000, preRes => {
			uni.hideLoading(preRes.code);
			if (preRes.code === 7000) {
				if (uni.getSystemInfoSync().platform === "ios") {
					jv.setCustomUIWithConfigiOS({
						navCustom: true,
						authPageBackgroundImage: "static/login_bg.png",
						logBtnText: "一键登录",
						logBtnTextColor: 0xFFFFFFFF,
						logBtnOffsetY: 460,
						privacyState: false,
						uncheckedImg: "static/login/radio.png",
						checkedImg: "static/login/active_radio.png",
						privacyCheckToastMessage: "请勾选隐私协议",
						privacyShowBookSymbol: true,
						privacyTextFontSize: 12,
						logoHidden: true,
						appPrivacyColor: [0xFF888888, 0xFF6E68E9],
						agreementNavBackgroundColor: 0xFFFFFFFF,
						agreementNavReturnImage: "static/login/goback.png",
						logBtnImgs: ["static/login/btn_bg.png",
							"static/login/btn_bg.png",
							"static/login/btn_bg.png"
						],

						addCustomViews: [{
								type: "label",
								id: "logoText",
								width: 120,
								height: 40,
								text: "天玩",
								textFont: 35,
								textColor: 0xff000000,
								left: 45,
								top: 90
							},
							{
								type: "label",
								id: "hmyzh",
								width: 70,
								height: 40,
								text: "还没有账号?",
								textFont: 11,
								textColor: 0xFF888888,
								top: 130,
								left: 45,
							},
							{
								type: "button",
								id: "ljzc",
								width: 70,
								height: 40,
								title: "立即注册",
								textFont: 11,
								textColor: 0xFF6E68E9,
								top: 130,
								left: 100,
								isFinish: false
							},
							{
								type: "label",
								id: "qtfsdl",
								width: 500,
								height: 40,
								text: "────────  其他登录方式  ────────",
								textFont: 11,
								textColor: 0xFF999999,
								bottom: -160,
								left: 75
							},
							{
								type: "imageView",
								id: "hydl",
								width: 122,
								height: 40,
								imagePath: "static/login/hydl.png",
								left: 120,
								top: 90
							},
							{
								type: "button",
								id: "number",
								width: 40,
								height: 40,
								normalImagePath: "static/login/number.png",
								bottom: -120,
								left: 180,
								isFinish: false
							},
							// {
							// 	type: "button",
							// 	id: "wechat",
							// 	width: 40,
							// 	height: 40,
							// 	normalImagePath: "static/login/wechat.png",
							// 	bottom: -120,
							// 	left: 230,
							// 	isFinish: false
							// },
							{
								type: "button",
								id: "goback",
								width: 35,
								height: 35,
								normalImagePath: "static/login/goHome.png",
								top: 40,
								left: 30
							}
						]
					})
				} else {
					jv.setCustomUIWithConfigAndroid({
						setAuthBGImgPathFromJs: "static/login_bg.png",
						setNavText: "", //	状态栏标题
						setPrivacyState: false,
						setStatusBarTransparent: true, //	是否设置状态栏透明
						setNavTransparent: true, //	导航栏背景是否透明
						setNavReturnImgPathFromJs: "static/login/goHome.png", //	导航栏返回图标
						setNavReturnBtnWidth: 30, //	导航栏宽度
						setNavReturnBtnHeight: 30, //	导航栏高度
						setLogoHidden: true, //	是否隐藏logo
						setLogBtnText: "一键登录", //	登录按钮文字
						setLogBtnImgPath: "static/login/btn_bg.png", //	授权登录按钮图片
						setNumberColor: 0xFF222222, //	手机号颜色
						setNumberSize: 25, //	手机号字体大小
						setLogBtnHeight: 45, //	登录按钮高度
						setLogBtnOffsetY: 300, //	登录按钮相对于标题栏下边缘y偏移
						setNumFieldOffsetY: 180, //	号码栏相对于标题栏下边缘y偏移
						setPrivacyTextCenterGravity: true, //	隐私条款相对于屏幕左边x轴偏移
						setPrivacyCheckboxSize: 13, //	隐私条款checkbox尺寸
						setPrivacyTextSize: 11, //隐私条款文字字体大小
						enableHintToast: true, //	checkbo没选中时是否提示用户
						setPrivacyWithBookTitleMark: true, //	隐私条款运营商协议名是否加书名号
						setAppPrivacyColor: [0xFF888888,
							0xFF6E68E9
						], //	隐私条款名称颜色(基础文字颜色，协议文字颜色)
						setPrivacyNavColor: 0xFF6E68E9, //	协议展示web页面导航栏背景颜色
						addCustomViews: [{
								type: "text",
								id: "logoText",
								width: 70,
								height: 40,
								text: "天玩",
								textSize: 35,
								textColor: 0xff000000,
								margins: [40, 10, 0, 0]
							},
							{
								type: "text",
								id: "hmyzh",
								width: 70,
								height: 40,
								text: "还没有账号?",
								textSize: 11,
								textColor: 0xFF888888,
								margins: [40, 65, 0, 0]
							},
							{
								type: "text",
								id: "ljzc",
								width: 70,
								height: 40,
								text: "立即注册",
								textSize: 11,
								textColor: 0xFF6E68E9,
								margins: [102, 65, 0, 0]
							},
							{
								type: "text",
								id: "qtfsdl",
								width: 500,
								height: 40,
								text: "────────  其他登录方式  ────────",
								textSize: 11,
								textColor: 0xFF999999,
								margins: [50, 505, 0, 0]
							},
							{
								type: "image",
								id: "hydl",
								width: 122,
								height: 40,
								bgImgPath: "static/login/hydl.png",
								margins: [115, 15, 0, 0],
							},
							{
								type: "image",
								id: "number",
								width: 40,
								height: 40,
								bgImgPath: "static/login/number.png",
								margins: [160, 535, 0, 0]
							},
							// {
							// 	type: "image",
							// 	id: "wechat",
							// 	width: 40,
							// 	height: 40,
							// 	bgImgPath: "static/login/wechat.png",
							// 	margins: [198, 535, 0, 0]
							// }
						],
					})
				}

				jv.loginAuth({
					autoFinish: true,
					timeout: 5000
				}, result => {
					// 结果监听
					let loginToken = result.content;
					if (result.code !== 6002) {
						getOaidAndImei().then(deviceInfo => {
							OneKeyLogin({
								deviceType: deviceInfo.type,
								deviceId: deviceInfo.id,
								loginToken,
								userSource: config.USER_SOURCE,
								registrationId: globalStore.registerID
							}).then(res => {
								resolve(res.data)
							})
						})
					}
					// 事件监听
				}, event => {
					// if(event.code === 1) {}
				})
			} else {
				let str = ""
				switch (preRes.code) {
					case 7002:
						str = "一键登录当日初始化次数达到上限,请稍后重试!"
						break;
				}

				uni.reLaunch({
					url: "/pages/login/index?str=" + str
				})
			}
		});
	})
}
// #endif

// #ifdef APP-PLUS
// 	自定义图标文字事件
jv.addCustomViewsClickCallback((id, callback) => {
	jv.dismissLoginAuth(true,
		() => {
			let url = ""
			switch (id) {
				case "ljzc":
					url = "/pages/login/register"
					break;
				case "number":
					url = "/pages/login/index"
					break;
					// case "wechat":
					// 	if (wechatCallback.value) wechatCallback.value()
					break
				default:
					// url = "/components/xzy-frame/index"
					console.log("关闭")
			}
			uni.reLaunch({
				url
			})
		})
})
// #endif